#! /usr/bin/python
# -*- coding:utf-8 -*-

## ========================================================================
## Copyright (C) 2007-2013  Jean-Philippe GOLAY
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
## ========================================================================

from persistance import *

class Sale:
  
  def __init__(self):
    self.mDB=ManageDB(Glob.dbName, Glob.user, Glob.passwd, Glob.host)
    logging.basicConfig(filename=Glob.LOG_FILENAME,level=logging.DEBUG)


  def read(self, id):
    if (self.mDB.executSQL(Glob.select_all_from_sale_where_id % id) == 0):
      sql=Glob.select_all_from_sale_where_id % id
      logging.debug(u'Sale: read() SQL error : %s' % sql)
      return 0
    else:
      result=self.mDB.resultSQL()
      if len(result) > 0:
        sale={}
        for row in result:
          sale[u'id']=row[0]
          sale[u'session_id']=row[1]
          sale[u'sale_date']=row[2]
          sale[u'sale_content']=row[3]
          sale[u'sale_price']=row[4]
          sale[u'sale_name']=row[5]
          sale[u'sale_addr']=row[6]
          sale[u'sale_email']=row[7]
          sale[u'sale_phone']=row[8]
          sale[u'sale_note']=row[9]
          sale[u'sale_pay']=row[10]
          sale[u'sale_date_pay']=row[11]
        return sale
      else:
        return 0


  def new(self, session_id, sale_content, sale_price, sale_name, sale_addr, sale_email, sale_phone, sale_note, sale_pay):
    u_sale_content=unicode(sale_content.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_sale_name=unicode(sale_name.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_sale_addr=unicode(sale_addr.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_sale_email=unicode(sale_email, u'utf8')
    u_sale_note=unicode(sale_note.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_sale_pay=unicode(sale_pay.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    
    if (self.mDB.executSQL(Glob.insert_sale % (session_id, u_sale_content, sale_price, u_sale_name, u_sale_addr, u_sale_email, sale_phone, u_sale_note, u_sale_pay) ) == 0):
      sql=Glob.insert_sale % (session_id, u_sale_content, sale_price, u_sale_name, u_sale_addr, u_sale_email, sale_phone, u_sale_note, u_sale_pay)
      logging.debug(u'Sale: new() SQL error : %s' % sql)
      return 0
    else:
      if (self.mDB.executSQL(Glob.last_insert_id_sale) == 0):
        logging.debug(u'Sale: new() SQL error : %s' % Glob.last_insert_id_sale)
        return 0
      else:
        result=self.mDB.resultSQL()
        for row in result:
          id=row[0]
        if id:
          return id 
        else:
          return 0


  def update(self, id, session_id, sale_content, sale_price, sale_name, sale_addr, sale_email, sale_phone, sale_note, sale_pay, sale_date_pay):
    u_sale_content=unicode(sale_content.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_sale_name=unicode(sale_name.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_sale_addr=unicode(sale_addr.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_sale_email=unicode(sale_email, u'utf8')
    u_sale_note=unicode(sale_note.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_sale_pay=unicode(sale_pay.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    
    if (self.mDB.executSQL(Glob.update_sale_where_id % (session_id, u_sale_content, sale_price, u_sale_name, u_sale_addr, u_sale_email, sale_phone, u_sale_note, u_sale_pay, sale_date_pay, id) ) == 0):
      sql=Glob.update_sale_where_id % (session_id, u_sale_content, sale_price, u_sale_name, u_sale_addr, u_sale_email, sale_phone, u_sale_note, u_sale_pay, sale_date_pay, id)
      logging.debug(u'Sale: update() SQL error : %s' % sql)
      return 0
    else:
      return 1


  def updateCustomer(self, id, sale_name, sale_addr, sale_email, sale_phone, sale_note ):
    u_sale_name=unicode(sale_name.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_sale_addr=unicode(sale_addr.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_sale_email=unicode(sale_email, 'utf8')
    u_sale_note=unicode(sale_note.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    
    if (self.mDB.executSQL(Glob.update_sale_cust_where_id % (u_sale_name, u_sale_addr, u_sale_email, sale_phone, u_sale_note, id) ) == 0):
      sql=Glob.update_sale_cust_where_id % (u_sale_name, u_sale_addr, u_sale_email, sale_phone, u_sale_note, id)
      logging.debug(u'Sale: updateCustomer() SQL error : %s' % sql)
      return 0
    else:
      return 1    


  def updatePay(self, id, sale_pay, sale_date_pay):
    u_sale_pay=unicode(sale_pay, u'utf8')
    
    if (self.mDB.executSQL(Glob.update_sale_pay_where_id % (u_sale_pay, sale_date_pay, id) ) == 0):
      sql=Glob.update_sale_pay_where_id % (u_sale_pay, sale_date_pay, id)
      logging.debug(u'Sale: updatePay() SQL error : %s' % sql)
      return 0
    else:
      return 1


  def delete(self, id):
    if (self.mDB.executSQL(Glob.delete_sale_where_id % id) == 0):
      sql=Glob.delete_sale_where_id % id
      logging.debug(u'Sale: delete() SQL error : %s' % sql)
      return 0
    else:
      return 1        
